package com.yangff.exception;

import com.yangff.entity.vo.ResultVo;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalDefaultExceptionHandler{

    @org.springframework.web.bind.annotation.ExceptionHandler(value = {Exception.class})
    public ResultVo handleException(Exception e) {
        log.error("服务器出了一个未设置的故障，内容如下： {}",e.getMessage());
        return ResultVo.custom("服务器出问题了，请联系管理员");
    }

    // 处理重复添加的异常
    @org.springframework.web.bind.annotation.ExceptionHandler(value = {DuplicateKeyException.class})
    public ResultVo DuplicateKeyException(Throwable e) {
        String msg = e.getMessage();
        msg = msg.substring(0, msg.indexOf("for key"));
        msg = msg.substring(msg.indexOf("'") + 1, msg.lastIndexOf("'"));
        return ResultVo.custom(msg + "已存在，换一个吧");
    }

}
